﻿
Imports std

Public Class input
    Private Shared ReadOnly q As qless(Of [case]) = Nothing
    Friend Shared Event case_poped(ByVal c As [case])

    Shared Sub New()
        q = New qless(Of [case])()
    End Sub

    Public Shared Function add(ByVal c As [case]) As eventComb
        Return New eventComb(Function() As Boolean
                                 If c Is Nothing Then
                                     Return False
                                 ElseIf drop_overflow_cases AndAlso q.size() >= queue_length Then
                                     Return False
                                 Else
                                     Return waitfor(Function() q.size() < queue_length) AndAlso
                                            goto_next()
                                 End If
                             End Function,
                             Function() As Boolean
                                 q.push(c)
                                 Return goto_end()
                             End Function)
    End Function

    Public Shared Function [get](ByVal waitfor_input As Boolean,
                                 ByVal p As pointer(Of [case])) As eventComb
        Dim c As [case] = Nothing
        Return New eventComb(Function() As Boolean
                                 If q.pop(c) Then
                                     assert(Not c Is Nothing)
                                     RaiseEvent case_poped(c)
                                     Return eva(p, c) AndAlso
                                            goto_end()
                                 ElseIf waitfor_input Then
                                     Return goto_next()
                                 Else
                                     Return False
                                 End If
                             End Function,
                             Function() As Boolean
                                 Return waitfor(Function() Not q.empty()) AndAlso
                                        goto_prev()
                             End Function)
    End Function

    Public Shared Function [get](ByVal p As pointer(Of [case])) As eventComb
        Return [get](True, p)
    End Function
End Class
